# Copyright (C) 2008 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
#

include $(TOPDIR)/rules.mk
## Package name
PKG_NAME:=yacas
## Package version we need (to match our download source)
PKG_VERSION:=1.2.2
## Release version (don't really needed, just for completeness)
PKG_RELEASE:=3
## Name of the file we will download, with the previous package name definitions
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
## Url with the PKG_SOURCE file
PKG_SOURCE_URL:=http://yacas.sourceforge.net/backups/
## Program to uncompress the sources
PKG_CAT:=zcat

include $(INCLUDE_DIR)/package.mk
# Definition of the package, for adding to menuconfig and creating the ipkg. 
define Package/yacas
	TITLE:=yacas
	SECTION:=Maths
	CATEGORY:=Maths
	URL:=http://yacas.sourceforge.net
endef

# Directory where the sources will be uncompiled and built BUILD_DIR
# is an env variable from the OpenWrt toolchain, you don't need to set
# it.

PKG_BUILD_DIR:=$(BUILD_DIR)/yacas-$(PKG_VERSION)

# Long description of the package

define Package/yacas/description
	YACAS is an easy to use, general purpose Computer Algebra System, a program for symbolic manipulation of mathematical expressions. It uses its own programming language designed for symbolic as well as arbitrary-precision numerical computations. The system has a library of scripts that implement many of the symbolic algebra operations; new algorithms can be easily added to the library. YACAS comes with extensive documentation (hundreds of pages) covering the scripting language, the functionality that is already implemented in the system, and the algorithms we used. 
endef

define Build/Prepare
# The following is a dirty hack. To compile yacas you need to get
# mkfastprimes to run in your host computer. To do so, in Prepare I
# download the tar.gz in pkg_build_dir/temporaryyacas to configure and
# make it to get this mkfastprimes compiled. configure has a problem:
# it generates the Makefile in the current working directory... and cd
# doesn't seem to change it. Thus, it would overwrite the Makefile in
# ports/misc/yacas, a thing which we don't like. To solve it, copy
# Makefile to Makefile2, configure-make and copy it back. The only
# drawback of this approach is that ports/misc/yacas/ gets a little
# full of miscellaneous, unneeded files, generated by configure. Once
# we have mkfastprimes, we copy it to tmp, and then the applied patch
# uses that file. It looks like pwd, export and several other
# constructs don't work under openwrt's build-make environment, that
# is why I just use $(CP) which looks like works correctly.
	wget $(PKG_SOURCE_URL)$(PKG_NAME)-$(PKG_VERSION).tar.gz
	mkdir /tmp/temporaryyacas
	mv $(PKG_NAME)-$(PKG_VERSION).tar.gz /tmp/temporaryyacas/
	cp Makefile Makefile2
	tar -C /tmp/temporaryyacas/ -xvf  /tmp/temporaryyacas/$(PKG_NAME)-$(PKG_VERSION).tar.gz 
	patch /tmp/temporaryyacas/$(PKG_NAME)-$(PKG_VERSION)/configure.in patches1/manmake.pat
	patch /tmp/temporaryyacas/$(PKG_NAME)-$(PKG_VERSION)/Makefile.in patches1/manmake2.pat
	/tmp/temporaryyacas/$(PKG_NAME)-$(PKG_VERSION)/configure 
	make 	
	$(CP) src/mkfastprimes /tmp/mkfastprimes
	cp Makefile2 Makefile
	rm /tmp/temporaryyacas/ -rf
	$(call Build/Prepare/Default)
endef

# Configure, in principle does not need any parameter

define Package/yacas/Build/Configure
	$(call Build/Configure/Default)
endef

# To end, copy the yacas binary to sbin, and all yacas scripts to
# sin/yacas-scripts. When you have installed the ipkg you have to add
# sin/yacas-scripts/ to the path, if you don't yacas won't do
# anything. You can also cd to sbin/yacas-scripts and run yacas from
# there. I'll have to check if configure or make has a configuration
# parameter to set the scripts folder.

define Package/yacas/install
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/src/yacas $(1)/usr/sbin/
	mkdir $(1)/usr/sbin/yacas-scripts/
	cp -r $(PKG_BUILD_DIR)/scripts/* $(1)/usr/sbin/yacas-scripts/
endef

# Final step for bulding the package

$(eval $(call BuildPackage,yacas))


